package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.coupon.dao.SeckillSessionDao;
import com.atguigu.gulimall.coupon.entity.SeckillSessionEntity;
import com.atguigu.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.atguigu.gulimall.coupon.service.SeckillSessionService;
import com.atguigu.gulimall.coupon.service.SeckillSkuRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page (
                new Query<SeckillSessionEntity> ().getPage (params),
                new QueryWrapper<SeckillSessionEntity> ()
        );

        return new PageUtils (page);
    }


    /**
     * 不仅查出这些活动、还应该查出这些活动里面上架的商品
     *
     * @return
     */
    @Override
    public List<SeckillSessionEntity> getLates3DaySession() {

        //计算最近三天的活动
        List<SeckillSessionEntity> list = this.list (new QueryWrapper<SeckillSessionEntity> ().
                between ("start_time", startTime (), endTime ()));
        if (list != null && list.size () > 0) {
            //查询出这个活动下的上架的商品
            List<SeckillSessionEntity> collect = list.stream ().map (session -> {
                Long id = session.getId (); //的到活动id
                //得到商品的集合-这些活动所关联的商品
                List<SeckillSkuRelationEntity> relationEntitys = seckillSkuRelationService.list (new QueryWrapper<SeckillSkuRelationEntity>
                        ().eq ("promotion_session_id", id));
                session.setRelationEntities (relationEntitys);
                return session;
            }).collect (Collectors.toList ());
            return collect;
        }
        return null;
    }


    private String startTime() {
        LocalDate now = LocalDate.now ();
        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of (now, min);
        String format = start.format (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss"));
        System.out.println ("startTime" + format);
        return format;
    }


    private String endTime() {
        LocalDate now = LocalDate.now ();
        LocalDate localDate = now.plusDays (2);
        LocalDateTime end = LocalDateTime.of (localDate, LocalTime.MAX);
        String format = end.format (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss"));
        System.out.println ("endTime" + format);
        return format;
    }

}
